﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;


namespace CuaHangLaptop.DAL
{
    class CTPhieuNhap_DAL
    {
        internal static int Get_Lasted_ID()
        {
            int id = 0;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select id from ChiTietPhieuNhap order by id desc", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                id = rdr.GetInt32(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return id;
        }

        // SELECT * FROM NHA SAN XUAT
        internal static List<CTPhieuNhap_BIZ> GetAll_CTPN()
        {
            List<CTPhieuNhap_BIZ> result = new List<CTPhieuNhap_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from ChiTietPhieuNhap", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    CTPhieuNhap_BIZ dd = new CTPhieuNhap_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // LẤY NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static CTPhieuNhap_BIZ Get_CTPhieuNhap_ByCode(string _maCTNhap)
        {
            CTPhieuNhap_BIZ ctpn1;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from ChiTietPhieuNhap where MaCTPhieuNhap='{0}'", _maCTNhap);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                ctpn1 = new CTPhieuNhap_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return ctpn1;
        }

        // LẤY TẤT CẢ NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static List<CTPhieuNhap_BIZ> GetAll_CTPhieuNhap_ByPN(string _maPN)
        {
            List<CTPhieuNhap_BIZ> result = new List<CTPhieuNhap_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from ChiTietPhieuNhap where MaPhieuNhap='{0}'", _maPN);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    CTPhieuNhap_BIZ dd = new CTPhieuNhap_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetString(3), rdr.GetInt32(4));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // DELETE
        internal static void Delete(CTPhieuNhap_BIZ ctpn)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from ChiTietPhieuNhap where id={0}", ctpn.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // UPDATE
        internal static void Update(CTPhieuNhap_BIZ ctpn)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update ChiTietPhieuNhap set MaCTPhieuNhap = '{0}', MaPhieuNhap = '{1}',MaLapTop = '{2}',SoLuong = {3} where id={4}",ctpn.MaCTphieunhap, ctpn.Phieunhap.MaPhieuNhap, ctpn.Laptop.MaLaptop, ctpn.Soluong, ctpn.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // INSERT
        internal static int Insert(CTPhieuNhap_BIZ ctpn)
        {
            int newId = -1;
            string s = "";
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                s = String.Format("insert into ChiTietPhieuNhap (MaCTPhieuNhap, MaPhieuNhap, MaLaptop, SoLuong) values('{0}','{1}','{2}',{3})", ctpn.MaCTphieunhap, ctpn.Phieunhap.MaPhieuNhap, ctpn.Laptop.MaLaptop, ctpn.Soluong);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    s = String.Format("select id from ChiTietPhieuNhap where MaCTPhieuNhap='{0}'", ctpn.ID);
                    cmd.CommandText = s;
                    rdr = cmd.ExecuteReader();

                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        newId = Int32.Parse(rdr["id"].ToString());
                    }
                }
            }            
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return newId;
        }
    }
}
